﻿using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Transactions;
using Newtonsoft.Json;
using T.FAS.Runtime.Metadata.Entity.DataModel;
using T.ServiceComponent.DAC.Service;
using T.FAS.Runtime.Base.Entity;
using T.FAS.Runtime.Base.Dao;

namespace T.FAS.Runtime.DataModel.Dao
{
    public class DataQuerySubQueryELBDao : DaoService<DataQuerySubQuery>
    { 
        public override string TableName => "FAS_DataQuerySubQuery";
        public override string IDColumnName => "ID";
        public string ForeignColumn => "DataQueryID";
        public override List<string> ExceptColumnName {get;} = DaoUtil.GetExceptionColumn<DataQuerySubQuery>();

        public DataQuerySubQueryELBDao() : base()
        {
            DBName = "TFAS";
        }

        /// <summary>
        /// 获取元数据内容
        /// </summary>
        /// <param name="metaDataId"></param>
        /// <returns></returns>
        public List<DataQuerySubQuery> GetDataQuerySubQuerys(string dqId)
        {
            var result = new List<DataQuerySubQuery>();
            if (string.IsNullOrEmpty(dqId))
            {
                return result;
            }
            List<object> objects = base.GetObjectsByFilter($"{ForeignColumn}='{dqId}'");
            if (objects != null)
            {
                objects.ForEach(obj => result.Add(obj as DataQuerySubQuery));
            }
            return result;
        }

        /// <summary>
        /// 添加DataQuerySubQuery
        /// </summary>
        /// <param name="dqSubQueries"></param>
        public void CreateDataQuerySubQuery(List<DataQuerySubQuery> dqSubQueries)
        {
            try
            {
                base.Add(dqSubQueries);
            }
            catch (Exception e)
            {
                throw new FasException(DataModelErrorCode.DQ.CreateSourceError, e.Message, e);
            }
        }

        /// <summary>
        /// 删除DataQuerySubQuery
        /// </summary>
        /// <param name="dqId"></param>
        public void DeleteDataQuerySubQuery(string dqId)
        {
            try
            {
                base.DB.ExecSqlStatement(string.Format(DaoUtil.DeleteSQLWithNoParam, this.TableName, this.ForeignColumn, dqId));
            }
            catch (Exception e)
            {
                throw new FasException(DataModelErrorCode.DQ.DeleteSourceError, e.Message, e);
            }
        }

        /// <summary>
        /// 删除DataQuerySubQuery
        /// </summary>
        /// <param name="dqIds"></param>
        public void BatchDeleteDataQuerySubQuery(List<string> dqIds)
        {
            try
            {
                List<string> sqls = new List<string>();
                foreach (string dqId in dqIds)
                {
                    sqls.Add(string.Format(DaoUtil.DeleteSQLWithNoParam, this.TableName, this.ForeignColumn, dqId));
                }
                base.DB.ExecSqlStatement(sqls.ToArray());
            }
            catch (Exception e)
            {
                throw new FasException(DataModelErrorCode.DQ.DeleteSourceError, e.Message, e);
            }
        }
    }
}
